package com.yjht.modules.sys.service.impl;

import com.yjht.common.constant.Constant;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yjht.common.utils.PageUtils;
import com.yjht.common.utils.Query;

import com.yjht.modules.sys.dao.SysAreaDao;
import com.yjht.modules.sys.entity.SysAreaEntity;
import com.yjht.modules.sys.service.SysAreaService;


@Service("sysAreaService")
public class SysAreaServiceImpl extends ServiceImpl<SysAreaDao, SysAreaEntity> implements SysAreaService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String parentId = (String) params.get("parentId");
        EntityWrapper<SysAreaEntity> ew = new EntityWrapper<>();
        SysAreaEntity condition = new SysAreaEntity();
        ew.setEntity(condition);
        if(StringUtils.isNotBlank(parentId)){
            ew.eq("parentid",Integer.parseInt(parentId));
        }
        Page<SysAreaEntity> page = this.selectPage(
                new Query<SysAreaEntity>(params).getPage(),
                ew
        );
        List<SysAreaEntity> list = page.getRecords();
        List<SysAreaEntity> result = new ArrayList<>();
        if(list != null && list.size() >0){
            for(SysAreaEntity entity : list){
                if(entity.getParentid() == Constant.top_area_id){
                    result.add(entity);
                }
            }
        }
        if(result.size() > 0){
            for(SysAreaEntity area : result){
                for(SysAreaEntity entity : list){
                    if(entity.getParentid() == area.getId()){
                        area.getChildren().add(entity);
                    }
                }
            }
        }
        page.setRecords(result);

        return new PageUtils(page);
    }

}
